<%@  language="VBScript" %>
<!-- #include file="aspuploader/include_aspuploader.asp" -->
<!-- #include file="adovbs.inc" -->

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
    <title>Photos Albums</title>
    <link rel="stylesheet" type="text/css" href="MainCSS.css" />
	<link href="demo.css" rel="stylesheet" type="text/css" />
	<link href="include/style100930.css" rel="stylesheet" type="text/css" />

	<!--Begin Uploader-->
	<script type="text/javascript">
		var handlerurl='ajax-handler.asp'
		var albumID= -1
		
		function CreateAjaxRequest()
		{
			var xh;
			if (window.XMLHttpRequest)
				xh = new window.XMLHttpRequest();
			else
				xh = new ActiveXObject("Microsoft.XMLHTTP");
			
			xh.open("POST", handlerurl, false, null, null);
			xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
			return xh;
		}
	</script>
	<script type="text/javascript">
	
	var fileArray=[];
	
	function ShowAttachmentsTable()
	{
		var table = document.getElementById("filelist");
		while(table.firstChild)table.removeChild(table.firstChild);
		
		AppendToFileList(fileArray);
	}
	function AppendToFileList(list)
	{
		var table = document.getElementById("filelist");
		
		for (var i = 0; i < list.length; i++)
		{
			var item = list[i];
			var row=table.insertRow(-1);
			row.setAttribute("fileguid",item.FileGuid);
			row.setAttribute("filename",item.FileName);
			var td1=row.insertCell(-1);
			td1.innerHTML="<img src='aspuploader/resources/circle.png' border='0'/>";
			var td2=row.insertCell(-1);
			td2.innerHTML=item.FileName;
			var td4=row.insertCell(-1);
			td4.innerHTML="[<a href='"+handlerurl+"?download="+item.FileGuid+"'>download</a>]";
			var td4=row.insertCell(-1);
			td4.innerHTML="[<a href='javascript:void(0)' onclick='Attachment_Remove(this)'>remove</a>]";
			
		}
	}
	
	function Attachment_FindRow(element)
	{
		while(true)
		{
			if(element.nodeName=="TR")
				return element;
			element=element.parentNode;
		}
	}
	
	function Attachment_Remove(link)
	{
		var row=Attachment_FindRow(link);
		if(!confirm("Are you sure you want to delete '"+row.getAttribute("filename")+"'?"))
			return;
		
		var guid=row.getAttribute("fileguid");
		
		var xh=CreateAjaxRequest();
		xh.send("delete=" + guid);

		var table = document.getElementById("filelist");
		table.deleteRow(row.rowIndex);
		
		for(var i=0;i<fileArray.length;i++)
		{
			if(fileArray[i].FileGuid==guid)
			{
				fileArray.splice(i,1);
				break;
			}
		}
	}
	
	function InsertDB(str)
	{

		
		var xh=CreateAjaxRequest();
		xh.send("InsertDB="+str);
	
	}
	
	function CuteWebUI_AjaxUploader_OnPostback()
	{
		var uploader = document.getElementById("myuploader");
		var guidlist = uploader.value;

		var xh=CreateAjaxRequest();
		xh.send("guidlist=" + guidlist);

		//call uploader to clear the client state
		uploader.reset();

		if (xh.status != 200)
		{
			alert("http error " + xh.status);
			setTimeout(function() { document.write(xh.responseText); }, 10);
			return;
		}

		var list = eval(xh.responseText); //get JSON objects
		
		fileArray=fileArray.concat(list);

		AppendToFileList(list);
	}
	
	</script>
	<!--End Uploader-->
    <script type="text/javascript">
        function getPhotos(albumID, userName, div) {
            populatePhotos(albumID, userName, div);
        }
    </script>
	
    <script type="text/javascript" src="MainJS.js"></script>
    <%
    Dim objConn, objRSCustomers, objRSAlbums, objRSPhotos, objRS
    Dim strUserName, strSQL, strResults, strDiv
    Dim iCustID
    Dim albumID, icol
	
    strDiv = "photoDiv"
    Response.Expires = -1500    
    'iCustID = Request.QueryString("custID")
	iCustID = 1

    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;UID=sa;PWD=MKkb1980;Initial Catalog=HomeTeamOnline;Server=."
    'objConn.ConnectionString = "dsn=Developer;uid=JohnNgo;pwd=jo2.0G$!;"    
    objConn.Open
    
	

	
    strSQL = "SELECT username FROM customers WHERE customerID = " & iCustID
    Set objRSCustomers = Server.CreateObject("ADODB.Recordset")
    objRSCustomers.Open strSQL, objConn
    Do While Not objRSCustomers.EOF
	    strUserName = objRSCustomers("username")
	    objRSCustomers.MoveNext
    Loop	
    objRSCustomers.Close
    set objRSCustomers = Nothing

    strSQL = "SELECT * FROM photos WHERE username = '" & strUserName & "'"
	set objRSPhotos = Server.CreateObject("ADODB.Recordset")
	objRSPhotos.Open strSQL, objConn, adOpenKeyset, adLockOptimistic, adCmdText
	
    strSQL = "SELECT * FROM photoAlbums WHERE username = '" & username = "'" & strUserName & "'"

%>
</td>
  </tr>
</table>
</td>
  </tr>
  <tr>
    <td>
	<br/>
	<%
		
		Dim uploader
		Set uploader=new AspUploader
		uploader.MaxSizeKB=10240
		uploader.Name="myuploader"
		uploader.MultipleFilesUpload=true
		uploader.InsertText="Upload Photos"
		
		uploader.AllowedFileExtensions="*.jpg,*.png,*.gif"
		uploader.SaveDirectory="Photos\" + strUserName
			
		%> 
		
	<%=uploader.GetString() %> </td>
  </tr>
  <tr>
    <td>     </td>
  </tr>
</table>

				

<script type="text/javascript">
function OnChange(dropdown)
{
    var myindex  = dropdown.selectedIndex
    var SelValue = dropdown.options[myindex].value
	
	albumID=dropdown.options[myindex].value
	
	var table = document.getElementById("filelist");
	while(table.firstChild)table.removeChild(table.firstChild);
	
	var uploader = document.getElementById("myuploader");
	

	if (SelValue!=''){

		var xh=CreateAjaxRequest();
		
		xh.send("OnChange=" + <%="'" + strUserName + "'" %> + "/" + SelValue);

		

		if (xh.status != 200)
		{
			alert("http error " + xh.status);
			setTimeout(function() { document.write(xh.responseText); }, 10);
			return;
		}

		var list = eval(xh.responseText); //get JSON objects
		
		fileArray=fileArray.concat(list);

		AppendToFileList(list);
		
    
	}
	else{
		var table = document.getElementById("filelist");
		while(table.firstChild)table.removeChild(table.firstChild);
	}
	return true;
}

</script>


<table id="filelist" style='border-collapse: collapse' class='Grid' border='0' cellspacing='0' cellpadding='2'>
			</table>
		
			
			<br/>
			<br/>

			
			
			<br/><br/>
			
			
			

	</div>
	
	<script type='text/javascript'>
	//this is to show the header..
	ShowAttachmentsTable();
	</script>
	<script type="text/javascript">
	function ShowFiles()
		{
			var msgs=[];
			for(var i=0;i<fileArray.length;i++)
			{
				msgs.push(fileArray[i].FileName);
			}
			alert(msgs.join("\r\n"));
		}
	</script>         	
                </div>
            </td>
        </tr>
    </table> 
	<script type='text/javascript'>
	function CuteWebUI_AjaxUploader_OnTaskComplete(task)
	{
		//alert(task.FileName + " is uploaded!");
	
		InsertDB( <%="'" + strUserName + "'" %> + "/" + task.FileName + "/" + albumID );
	}
	</script>

	<%
	objRSAlbums.Close
	set objRSAlbums = Nothing
	objRSPhotos.Close
	set objRSPhotos = Nothing
	objConn.Close
	set objConn = Nothing
	%>
</body>
</html>